use tokio::net::TcpStream;
use async_trait::async_trait;
use std::any::Any;

use crate::{actor::ActorRef, message::Message};
use super::{
    errors::{RemoteError, SendError},
    transport::Connection,
    batch::BatchManager,
    serialization::MessageSerializer,
};

pub struct TcpConnection {
    stream: TcpStream,
    batch_manager: BatchManager,
    serializer: MessageSerializer,
}

impl TcpConnection {
    pub fn new(stream: TcpStream) -> Self {
        Self {
            stream,
            batch_manager: BatchManager::new(),
            serializer: MessageSerializer::new(),
        }
    }

    pub async fn handle(self) {
        // 实现连接处理逻辑
        while let Ok(message) = self.receive().await {
            // 处理消息
        }
    }
}

#[async_trait]
impl Connection for TcpConnection {
    async fn send<T: Any + Send + Clone>(&mut self, target: &ActorRef, message: Message<T>) -> Result<(), SendError> {
        self.batch_manager.add_message(target, message).await
    }

    async fn receive(&mut self) -> Result<Box<dyn Any + Send>, RemoteError> {
        // 实现消息接收逻辑
        todo!("Implement message receiving")
    }
} 